﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Autofac;
using Autofac.Integration.Mvc;
using ClientManager.Data;
using ClientManager.Data.Infrastucture;
using ClientManager.Service.Clients;
using ClientManager.Service.ServiceLevels;

namespace ClientManager.Web.App_Start
{
    public class AutofacConfig
    {
        public static void RegisterAutofac()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            RegisterDataLayerDependecies(builder);
            RegisterBusinessLayerDependencies(builder);
            RegisterPresentationLayerDependencies(builder);
            var container = builder.Build();


            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }

        private static void RegisterPresentationLayerDependencies(ContainerBuilder builder)
        {
        }

        private static void RegisterBusinessLayerDependencies(ContainerBuilder builder)
        {
            builder.RegisterType<ClientService>().As<IClientService>();
            builder.RegisterType<ServiceLevelService>().As<IServiceLevelService>();
        }

        private static void RegisterDataLayerDependecies(ContainerBuilder builder)
        {
            builder.RegisterGeneric(typeof(GenericRepository<>)).As(typeof(IRepository<>)).InstancePerDependency();
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
            builder.RegisterType<ClientManagerDbContext>().As<DbContext>();
        }

    }
}